{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from operator import add\n",
    "from pyspark import SparkContext\n",
    "from pyspark.streaming import StreamingContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = SparkContext.getOrCreate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "ssc = StreamingContext(sc, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------------------------------------\n",
      "Time: 2019-07-23 09:05:36\n",
      "-------------------------------------------\n",
      "(4, 100)\n",
      "(8, 100)\n",
      "(0, 100)\n",
      "(1, 100)\n",
      "(5, 100)\n",
      "(9, 100)\n",
      "(2, 100)\n",
      "(6, 100)\n",
      "(3, 100)\n",
      "(7, 100)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 09:05:38\n",
      "-------------------------------------------\n",
      "(4, 100)\n",
      "(8, 100)\n",
      "(0, 100)\n",
      "(1, 100)\n",
      "(5, 100)\n",
      "(9, 100)\n",
      "(2, 100)\n",
      "(6, 100)\n",
      "(3, 100)\n",
      "(7, 100)\n",
      "\n",
      "-------------------------------------------\n",
      "Time: 2019-07-23 09:05:40\n",
      "-------------------------------------------\n",
      "(4, 100)\n",
      "(8, 100)\n",
      "(0, 100)\n",
      "(1, 100)\n",
      "(5, 100)\n",
      "(9, 100)\n",
      "(2, 100)\n",
      "(6, 100)\n",
      "(3, 100)\n",
      "(7, 100)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    rddQueue = []\n",
    "    for i in range(5):\n",
    "        rddQueue += [ssc.sparkContext.parallelize([j for j in range(1, 1001)], 10)]\n",
    " \n",
    "    # Create the QueueInputDStream and use it do some processing\n",
    "    inputStream = ssc.queueStream(rddQueue)\n",
    "    mappedStream = inputStream.map(lambda x: (x % 10, 1))\n",
    "    reducedStream = mappedStream.reduceByKey(lambda a, b: a + b)\n",
    "    reducedStream.pprint()\n",
    " \n",
    "    ssc.start()\n",
    "    # 延迟20s后结束程序\n",
    "    time.sleep(20)\n",
    "    ssc.stop(stopSparkContext=True, stopGraceFully=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
